package com.example.commenlibary.ui.ListDialog;

import java.lang.reflect.Method;
import java.util.ArrayList;

import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.example.dexfiletest.DexFileTest;
import com.example.dexfiletest.R;

public class SelectListDialog extends PopupWindow{
	private View mainView;
	private Context context;
	private ListView listView;
	private SelectListAdapter selectListAdapter;
	private Resources mResources;
//	private ArrayList<String> selectList;
	public static boolean isDL = true;
	public SelectListDialog(Context context,int width,ArrayList<String> selectList)
	{
		super(context);
	//	getResource(context);
		this.context = context;
		
		if(isDL){
			Resources resource = DexFileTest.createResources(context);
			XmlResourceParser parser = resource.getLayout(R.layout.select_list_dialog);
			//mainView = ((Activity)context).getLayoutInflater().inflate(R.layout.select_list_dialog, null);
			LayoutInflater layoutInflater =  ((Activity)context).getLayoutInflater();
			//layoutInflater.in
			mainView = layoutInflater.inflate(parser, null);
		}else{
			mainView = ((Activity)context).getLayoutInflater().inflate(R.layout.select_list_dialog, null);
		}
		
		listView = (ListView)mainView.findViewById(R.id.SelectListDialog_List);
		listView.getLayoutParams().width = width;
		selectListAdapter = new SelectListAdapter(context, selectList);
		listView.setAdapter(selectListAdapter);
		this.setContentView(mainView);
		setWidth(width);
		setHeight(LayoutParams.WRAP_CONTENT);
		this.setFocusable(true);
		
		//setBackgroundDrawable(context.getResources().getDrawable(R.drawable.));
	}
	
	
	public void setWidgetWidth(int width)
	{
		listView.getLayoutParams().width = width;
		setWidth(width);
	}
	public void setListViewBackground(int drawable)
	{
		listView.setBackgroundResource(drawable);
	}
	public void setCommenLibaryMessageOperator(CommenLibaryMessageOperator listener)
	{
		selectListAdapter.setCommenLibaryMessageOperator(listener);
	}
	
	public void setDeleteListener(CommenLibaryMessageOperator delectListener)
	{
		selectListAdapter.setDeleteListener(delectListener);
	}
	public void ShowDialog(View showView){
		if(!isShowing()){
			this.showAsDropDown(showView);
		}
	}
	
	
	public void CloseDialog()
	{
		if(isShowing()){
			dismiss();
		}
	}

	
	public void addSelectObject(String object)
	{
		selectListAdapter.addObject(object);
	}


	
	private void getResource(String mDexPath,Context context){
		AssetManager mAssetManager = null;
		try {  
			AssetManager assetManager = AssetManager.class.newInstance();  
			Method addAssetPath = assetManager.getClass().getMethod("addAssetPath", String.class);  
			addAssetPath.invoke(assetManager, mDexPath);  
			mAssetManager = assetManager;  
		} catch (Exception e) {  
			e.printStackTrace();  
		}  
		Resources superRes = context.getResources();  
		mResources = new Resources(mAssetManager, superRes.getDisplayMetrics(),  
				superRes.getConfiguration());
	}
}
